﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DB = HTComics.Data;

namespace HTComics.Web.Admin
{
    public partial class ForexRates : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["IsAdmin"] == null || !Convert.ToBoolean(Session["IsAdmin"]))
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadForexRates();
        }

        private void LoadForexRates()
        {
            DataTable dtForexRate = DB.Misc.GetForexRate();

            txtRate.Value = string.Format("{0:0.00}", dtForexRate.Rows[0]["Rate"]);
            lblLastUpdated.Text = Convert.ToDateTime(dtForexRate.Rows[0]["Timestamp"].ToString()).ToString("MMM dd yyyy");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DB.Admin.UpdateForexRate(txtRate.Value);
            Application["ForexRate"] = Convert.ToDouble(txtRate.Value);

            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>success();</script>");
        }
    }
}